package com.app.newcsc.fragment.my.task;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.app.newcsc.BR;
import com.app.newcsc.R;
import com.app.newcsc.app.AppApplication;
import com.app.newcsc.app.AppViewModelFactory;
import com.app.newcsc.databinding.FragmentMyTaskBinding;
import com.app.newcsc.entity.Message;
import com.app.newcsc.entity.TaskInfoEntity;
import com.app.newcsc.entity.WalletEntity;
import com.app.newcsc.fragment.BaseFragmentPagerAdapter;
import com.app.newcsc.utils.NumberUtils;
import com.app.newcsc.utils.SpUtils;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import io.reactivex.functions.Consumer;
/**
 * @author: woking
 * @date: 2021/6/9
 * @description：
 */
public class TaskFragment extends BaseFragment<FragmentMyTaskBinding, TaskViewModel> {

    private List<Fragment> mFragments;
    private List<String> titlePager;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_my_task;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public TaskViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(TaskViewModel.class);
    }

    @Override
    public void initData() {
        SpUtils.put(getActivity(),"isLogin","1");
        WalletEntity mWalletEntity = ((AppApplication)AppApplication.getInstance()).getWalletEntityType("AD");
        if(mWalletEntity != null)
        {
            double adNum = Double.valueOf(mWalletEntity.money)-Double.valueOf(mWalletEntity.lock_s_one);
            binding.adSell.setText(NumberUtils.keepPrecision(String.valueOf(adNum),2));
        }

        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        initSubscription();
        mFragments = pagerFragment();
        titlePager = pagerTitleString();
        //设置Adapter
        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        binding.viewPager.setAdapter(pagerAdapter);
        binding.viewPager.setOffscreenPageLimit(10);
        binding.tabs.setupWithViewPager(binding.viewPager);
        binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabs));

        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

//        viewModel.otherAssets();
//        viewModel.financeProduct();
//        viewModel.financeProductDetil("9");
//        viewModel.financeProductOrders();
//        viewModel.createFinanceProductOrder();
//        viewModel.financeProductOrderDetails();
        viewModel.mTaskInfoEntity.observe(this, new Observer<TaskInfoEntity>() {
            @Override
            public void onChanged(TaskInfoEntity taskInfoEntity) {

            }
        });

    }

    @Override
    public void initViewObservable() {

    }

    protected List<Fragment> pagerFragment() {
        List<Fragment> list = new ArrayList<>();
        list.add(TaskTabFragment.newInstance(0));
        list.add(TaskTabFragment.newInstance(1));
        list.add(TaskTabFragment.newInstance(2));
        return list;
    }

    protected List<String> pagerTitleString() {
        List<String> list = new ArrayList<>();
        list.add("运行中");
        list.add("任务商城");
        list.add("已到期");
        return list;
    }

    private Disposable mSubscription;
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mSubscription != null)
        {
            RxSubscriptions.remove(mSubscription);
        }
    }
    private void initSubscription()
    {
        mSubscription = RxBus.getDefault().toObservable(Message.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(new Consumer<Message>() {
                    @Override
                    public void accept(final Message message) throws Exception {
                        Log.e("TAG","===============message=============="+message.getName());
                        if(message.getName().equals("RefreshExchangeTask"))
                        {
                            viewModel.otcWallet("");//钱包余额
                        }
                    }
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);

        viewModel.mWalletEntitys.observe(this, new Observer<List<WalletEntity>>() {
            @Override
            public void onChanged(List<WalletEntity> walletEntities) {
                WalletEntity mWalletEntity = ((AppApplication)AppApplication.getInstance()).getWalletEntityType("AD");
                if(mWalletEntity != null)
                {
                    binding.adSell.setText(NumberUtils.keepPrecision(mWalletEntity.money,2));
                }
            }
        });
    }

}
